<?php

namespace app\common\model\banxi\learn;

use app\common\enums\AccountTypeEnum;
use app\common\model\common\BaseModel;
use think\model\concern\SoftDelete;

/**
 * 上课记录
 */
class LearnHearingRecord extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_learn_hearing_record';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'accountTypeCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'skRecordId' => 'string',
        'tutorTrainerId' => 'string',
        'trainerId' => 'string',
        'studentId' => 'string',
        'courseId' => 'string',
        'isSubmit' => 'boolean',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];


    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                       ｜
    //-----------------------------------------------------------------------------------

    public function getAccountTypeCnAttr($value, $data)
    {
        $value = $value ?: ($data['accountType'] ?? '');
        return (new AccountTypeEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    /**
     * 根据记录ID和账户信息获取听力记录
     *
     * @param int $recordId 记录ID
     * @param int|null $studentId 学生ID
     * @param int|null $trainerId 教练ID
     * @return array|null
     */
    public function getByIdAndAccount($recordId, $studentId, $trainerId)
    {
        $query = $this->where('id', $recordId);

        // 如果学生ID不为空，则添加学生ID条件
        if (!empty($studentId)) {
            $query->where('studentId', $studentId);
        }

        // 如果教练ID不为空，则添加教练ID或辅导教练ID条件
        if (!empty($trainerId)) {
            $query->where(function ($query) use ($trainerId) {
                $query->where('trainerId', $trainerId)
                    ->whereOr('tutorTrainerId', $trainerId);
            });
        }

        return $query->find();
    }


    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------
}